package com.Example;

import com.alibaba.fastjson.JSONObject;
import com.client.RepChainClient;
import com.crypto.ECDSASign;
import com.crypto.Sha256;
import com.crypto.SignFunc;
import com.google.protobuf.ByteString;
import com.protos.Peer.*;
import com.crypto.BitcoinUtils;
import com.utils.CertUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Obtain {
    //    String host = "192.168.2.69:8081";
    private static String host = "localhost:8081";
    private static RepChainClient client = new RepChainClient(host);

    public static void main(String[] args) throws Exception{
        // 流式获取块
        Block blockStream = client.getBlockStreamByHeight(1);
        // 根据txid流式获取交易
        Transaction tranStream = client.getTransactionStreamByTxid("2e3db7ca-7eb0-4377-bf29-4fc8a86941a3");
        // 获取块链信息
        JSONObject chainInfo = client.getChainInfo();
        System.out.println(chainInfo);
        // 根据txid获取交易
        Transaction tran = client.getTransactionByTxid("5bd04561-7efa-47ad-b07d-baf98843e71b");
        // 获得块高度
        int height = chainInfo.getJSONObject("result").getIntValue("height");
        // 根据高度获取块数据
        Block block = client.getBlockByHeight(height);
        System.out.println(block);  // 打印块
        // currentBlockHash可以查询本块
        Block currentBlock = client.getBlockByBlockHash(chainInfo.getJSONObject("result").getString("currentBlockHash"));
        // previousBlockHash可以查询上一个块
        Block previousBlock = client.getBlockByBlockHash(Base64.encodeBase64String(block.getPreviousBlockHash().toByteArray()));
        String previousBlockHash = chainInfo.getJSONObject("result").getString("previousBlockHash");
//        JSONObject previousBlockJsonObject = client.doGet("http://" + host + "/block/hash/"+previousBlockHash);
//        JSONObject result = previousBlockJsonObject.getJSONObject("result");
//        String json = result.toJSONString();
//        Block.Builder builder = Block.newBuilder();
//        JsonFormat.parser().merge(json,builder);
//        Block preBlock = builder.build();
        Block preBlock = client.getBlockByBlockHash(previousBlockHash);
//                System.out.println(preBlock);
        //对block进行验证，对block进行hash计算
        //验证previous block的hash值
        byte[] blockHash = Sha256.hash(preBlock.toByteArray());
        String blockHash1 = Hex.encodeHexString(blockHash);
        String blockHash2 = Base64.encodeBase64String(block.getPreviousBlockHash().toByteArray());
        byte[] blockHash3 = Base64.decodeBase64(previousBlockHash);
        System.out.println(new String(blockHash3));
        System.out.println(blockHash2.equals(previousBlockHash));
        System.out.println(blockHash1.equals(block.getPreviousBlockHash().toStringUtf8()));
        System.out.println(blockHash1.equals(new String(blockHash3)));

    }
}
